package com.hxzy.tms.aciton;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.apache.commons.lang3.StringUtils;

import com.hxzy.tms.entity.Attachment;
import com.hxzy.tms.service.AttachmentService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class DownloadAction extends AbstractAction {
    private static final long serialVersionUID = -6919371189389613409L;

    private AttachmentService attachmentService;

    private String filename;

    private String contentType;

    private long contentLength;

    private String contentDisposition; // attachment;filename="${filename}"

    private InputStream inputStream;

    private String id;

    @Override
    public String execute() throws Exception {
        if (StringUtils.isBlank(id)) {
            addActionError("下载文件参数错误");
            return Action.ERROR;
        }

        Attachment attachment = this.attachmentService.get(Long.valueOf(id));
        if (attachment == null) {
            addActionError("附件不存在");
            return Action.ERROR;
        }
        setContentType(attachment.getContentType());
        setContentLength(attachment.getSize());
        if ("image/png".equals(contentType)) {
            setContentDisposition(String.format("inline;filename=\"%s\"", getFilename()));
        } else {
            setContentDisposition(String.format("attachment;filename=\"%s\"", getFilename()));
        }

        return SUCCESS;
    }

    public InputStream getInputStream() throws FileNotFoundException {
        return this.attachmentService.download(Long.parseLong(id));
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public String getFilename() {
        Attachment attachment = this.attachmentService.get(Long.valueOf(id));
        try {
            return URLEncoder.encode(attachment.getFilename(), "utf-8");
        } catch (UnsupportedEncodingException e) {
            logger.error("下载文件处理编码失败", e);
        }
        return System.currentTimeMillis() + "";
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public long getContentLength() {
        return contentLength;
    }

    public void setContentLength(long contentLength) {
        this.contentLength = contentLength;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getContentDisposition() {
        return contentDisposition;
    }

    public void setContentDisposition(String contentDisposition) {
        this.contentDisposition = contentDisposition;
    }

    public AttachmentService getAttachmentService() {
        return attachmentService;
    }

    public void setAttachmentService(AttachmentService attachmentService) {
        this.attachmentService = attachmentService;
    }

}
